const {
  systemPreferences,
  desktopCapturer,
  app,
  BrowserWindow,
  ipcMain,
} = require("electron");

const loudness = require("loudness");

const useExec = require("child_process");
const execVolume = useExec.exec;
const { join } = require("path");
const path = require("path");
const baseDir = path.resolve("src/assets");

const loadNircmd = join(baseDir, "nircmd.exe");
const { machineIdSync } = require("node-machine-id");
let win;
let mainWindow;
let deviceId;

const createLoginWindow = () => {
  win = new BrowserWindow({
    width: 800,
    height: 600,
    title: "Login",
    icon: join(__dirname, "../public/desk_img.png"),
    frame: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
      webSecurity: true,
    },
  });

  win.on("maximize", () => {
    return win.unmaximize();
  });

  win.on("closed", () => {
    win = null;
  });

  if (process.env.VITE_DEV_SERVER_URL) {
    win.loadURL(process.env.VITE_DEV_SERVER_URL);
    win.webContents.openDevTools();
  } else {
    win.loadFile(join(__dirname, "../dist/index.html"));
    win.webContents.openDevTools();
  }
};

const createMainWindow = () => {
  mainWindow = new BrowserWindow({
    width: 1600,
    height: 986,
    title: "直播页",
    icon: join(__dirname, "../public/desk_img.png"),
    frame: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
      webSecurity: true,
    },
  });
  
  if (process.env.VITE_DEV_SERVER_URL) {
    mainWindow.loadURL(process.env.VITE_DEV_SERVER_URL + "#/home");
    mainWindow.webContents.openDevTools();
  } else {
    mainWindow.loadFile(join(__dirname, "../dist/index.html"), {
      hash: "home",
    });
    mainWindow.webContents.openDevTools();
  }

  mainWindow.on("closed", () => {
    mainWindow = null;
  });

  ipcMain.on("process-all", async (event) => {
    let resouresRes = await desktopCapturer.getSources({
      types: ["window", "screen"],
    });
    event.sender.send("getProcess", resouresRes);
  });
};

app.whenReady().then(() => {
  deviceId = machineIdSync();
  createLoginWindow();
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});

app.on("activate", () => {
  if (!win && !mainWindow) {
    createLoginWindow();
  }
});

ipcMain.on("login-success", () => {
  if (win) {
    win.close();
  }
  createMainWindow();
});

ipcMain.on("login-out", () => {
  if (mainWindow) {
    mainWindow.close();
  }
  createLoginWindow();
});

ipcMain.on("windows_close", () => {
  app.quit();
});
ipcMain.on("windows_fullscreen", () => {
  mainWindow.setFullScreen(true);
});
ipcMain.on("windows_setmaximize", () => {
  mainWindow.setFullScreen(false);
});
ipcMain.on("windows_minimize", () => {
  mainWindow.minimize();
});
ipcMain.on("login_minimize", () => {
  win.minimize();
});

ipcMain.on("getDeviceId", (event) => {
  event.sender.send("returnDeviceId", deviceId);
});

function setSysVolu(volume) {
  execVolume(`${loadNircmd} setsysvolume ${655.35 * volume}`, (x, y, z) => {});
}
function getSysVolu() {
  return new Promise((resolve) => {
    loudness.getVolume().then((res) => {
      resolve(res);
    });
  });
}
ipcMain.on("getSysVolu", (event) => {
  getSysVolu().then((res) => {
    event.sender.send("sysVoluVal", res);
  });
});
ipcMain.on("setSysVolu", (event, val) => {
  setSysVolu(val);
});
